package com.ayoka.ui.main;

import android.graphics.Color;
import android.util.Log;
import android.view.View;

import com.ayoka.Constant;
import com.ayoka.R;
import com.ayoka.base.BaseFragment;
import com.ayoka.bean.UserInformation;
import com.ayoka.databinding.FragmentMineBinding;
import com.ayoka.mvp.MineMVP.IMineView;
import com.ayoka.mvp.MineMVP.MinePresenter;
import com.ayoka.ui.member.OpenMemberActivity;
import com.ayoka.ui.mine.coupon.CouponActivity;
import com.ayoka.ui.customer_service.CustomerServiceRecyclerViewActivity;
import com.ayoka.ui.mine.AboutUsActivity;
import com.ayoka.ui.mine.collection.CollectionActivity;
import com.ayoka.ui.mine.purchase_record.PurchaseRecordActivity;
import com.ayoka.ui.mine.setting.SettingActivity;
import com.ayoka.ui.mine.use_help.UseHelpActivity;
import com.ayoka.utils.MemberEvent;
import com.jcp.baselibrary.utils.StringUtil;
import com.jincp.image.load.ImageLoaderOptions;
import com.jincp.image.load.ImageLoaderProxy;

import java.util.Observable;

/**
 * 我的Fragment
 * <p>
 * Created by admin on 2017/3/22.
 */

public class MineFragment extends BaseFragment<FragmentMineBinding, MinePresenter> implements IMineView {

    @Override
    public int getContentLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initViews(View view) {
        mDataBinding.ivCustomerService.setColorFilter(Color.WHITE);
        mDataBinding.btnOpenMember.setOnClickListener(this);
        mDataBinding.tvCheckRight.setOnClickListener(this);
        mDataBinding.ivCustomerService.setOnClickListener(this);
        mDataBinding.tvFavorite.setOnClickListener(this);
        mDataBinding.tvCoupon.setOnClickListener(this);
        mDataBinding.tvPurchaseRecord.setOnClickListener(this);
        mDataBinding.tvAboutUs.setOnClickListener(this);
        mDataBinding.tvUseHelp.setOnClickListener(this);
        mDataBinding.tvSetting.setOnClickListener(this);
    }

    @Override
    public void initData() {
        configUserInfo();
        mPresenter.getUserInfo();

        if (UserInformation.getInstance().user_info.IsVip == 0){
            MemberEvent.getInstance().addObserver(this);
        }
    }

    @Override
    protected MinePresenter getPresenter() {
        return new MinePresenter();
    }

    @Override
    public void configUserInfo() {
        if (UserInformation.getInstance().user_info == null) return;
        ImageLoaderProxy.getInstance().displayImage(UserInformation.getInstance().user_info.LocalAvatar, mDataBinding.ivUserHeader, ImageLoaderOptions.getCircleOptions());
        mDataBinding.tvUserName.setText(UserInformation.getInstance().user_info.WeixinNickName);
        if (UserInformation.getInstance().user_info.IsVip == 0){
            mDataBinding.layoutNotMember.setVisibility(View.VISIBLE);
            mDataBinding.layoutWasMember.setVisibility(View.GONE);
        } else {
            mDataBinding.layoutNotMember.setVisibility(View.GONE);
            mDataBinding.layoutWasMember.setVisibility(View.VISIBLE);
            mDataBinding.tvMemberType.setText(StringUtil.formatString(R.string.format_member_type, "基础会员"));
            mDataBinding.tvExpiryDate.setText(StringUtil.formatString(R.string.format_expiry_date, UserInformation.getInstance().user_info.VipExpiredDate));
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mDataBinding.ivCustomerService) {
            CustomerServiceRecyclerViewActivity.start(getActivity());
        }
        if (view == mDataBinding.btnOpenMember) {
            OpenMemberActivity.start(getActivity(), Constant.OpenMember.OPEN_MEMBER);
        }
        if (view == mDataBinding.tvCheckRight) {
            OpenMemberActivity.start(getActivity(), Constant.OpenMember.CHECK_RIGHT);
        }
        if (view == mDataBinding.tvCoupon) {
            CouponActivity.start(getActivity());
        }
        if (view == mDataBinding.tvPurchaseRecord) {
            PurchaseRecordActivity.start(getActivity());
        }
        if (view == mDataBinding.tvFavorite) {
            CollectionActivity.start(getActivity());
        }
        if (view == mDataBinding.tvUseHelp) {
            UseHelpActivity.start(getActivity());
        }
        if (view == mDataBinding.tvSetting) {
            SettingActivity.start(getActivity());
        }
        if (view == mDataBinding.tvAboutUs) {
            AboutUsActivity.start(getActivity());
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        super.update(observable, data);
        if (observable instanceof MemberEvent){
            configUserInfo();
        }
    }
}
